package cn.study.springsecurityoauth2.service;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 登陆的权限认证逻辑
 * @ClassName MyUserService
 * @Author swj
 * @Date 2020/9/9 20:10
 */
@Component
public class MyUserDetailService implements UserDetailsService {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        logger.info("登陆用户名:{}",s);
        String password  = "123";
        password = passwordEncoder.encode(password);
        List<SimpleGrantedAuthority> authorities = new ArrayList<>(8);
        //SimpleGrantedAuthority authority = new SimpleGrantedAuthority("user");
        //authorities.add(authority);
        SimpleGrantedAuthority authority2 = new SimpleGrantedAuthority("ROLE_admin");
        authorities.add(authority2);
        return new User(s,password, true, true, true, true,
                authorities);
    }
}
